<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//商品管理控制器
class GoodsController extends CommonController{
	private $goods_model;
	private $cate_model;
	private $brand_model;
	private $detail_model;
	private $typeattr_model;
	private $goodsattr_model;
	public function __init(){
		//调用CommomControlller里面的构造函数，包含验证(是否登录)
		parent::__init();
		//实例化Goods模型
		$this->goods_model = new \Common\Model\Goods;
		//实例化Cate模型
		$this->cate_model = new \Common\Model\Cate;
		//实例化Brand模型
		$this->brand_model = new \Common\Model\Brand;
		//实例化Detail模型
		$this->detail_model = new \Common\Model\Detail;
		//实例化TypeAttr模型
		$this->typeattr_model = new \Common\Model\TypeAttr;
		//实例化GoodsAttr模型
		$this->goodsattr_model = new \Common\Model\GoodsAttr;
	}
	//展示商品
	public function index(){
		//1.统计总条数
		$total = $this->goods_model->join('category','category_cid','=','cid')->count();
		//2.分页类使用
		$page = Page::row(10)->make($total);
		//分配页码
		View::with('page',$page);
		$data = $this->goods_model->join('category','category_cid','=','cid')->limit(Page::limit())->orderBy('gid','DESC')->get();
		//3.分配数据
		View::with('data',$data);
		//载入模板
		View::make();
	}
	//添加商品
	public function add(){
		//3.添加
		if(IS_POST){
			//添加成功
			if($this->goods_model->store()) View::success('添加成功',U('index'));
			View::error($this->goods_model->getError());
		}
		//1.处理"所属分类"
		$cateData = $this->cate_model->get();
		$cateData = Data::tree($cateData,'cname','cid');
		//分配数据
		View::with('cateData',$cateData);
		//2.处理"所属品牌"
		$brandData = $this->brand_model->get();
		//分配数据
		View::with('brandData',$brandData);	
		//载入模板
		View::make();
	}
	//上传列表图
	public function upload_list()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	//上传详细页图册
	public function upload_most()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	//添加(异步请求)
	public function ajaxTypeAttr(){				
		$tid = Q('post.id',0,'intval');
		$data = Db::table('typeattr')->where("type_tid={$tid}")->get();
		foreach ($data as $k => $v) {
			$data[$k]['tavalue'] = explode('|', $v['tavalue']);
		}
		echo json_encode($data);exit;
	}
	//编辑商品
	public function edit(){
		//5.修改
		if(IS_POST){
			//修改成功
			if($this->goods_model->edit()) View::success('修改成功',U('index'));
			View::error($this->goods_model->getError());
		}
		//1.得到商品ID
		$gid = Q('get.gid',0,'intval');
		//2.处理"所属分类"
		$cateData = $this->cate_model->get();
		$cateData = Data::tree($cateData,'cname','cid');
		//分配数据
		View::with('cateData',$cateData);
		//3.处理"所属品牌"
		$brandData = $this->brand_model->get();
		//分配数据
		View::with('brandData',$brandData);
		//4.得到旧数据
		$goodsOldData = $this->goods_model->where("gid={$gid}")->get();
		//分配数据
		View::with('goodsOldData',$goodsOldData);
		//5.得到详细数据
		//找到一条用find(),得到一维数组
		$detailOldData = $this->detail_model->where("goods_gid={$gid}")->find();
		//p($detailOldData);
		if(!empty($detailOldData)){
			$detailOldData['detail_pic'] = explode('|', $detailOldData['detail_pic']);
		}
		//p($detailOldData['detail_pic']);exit;
		
		//6.得到类型属性
		$tid = $goodsOldData[0]['type_tid'];
		//$tattrOldData = $this->tattr_model->where("type_tid={$tid}")->get();
		$goodsAttrData = $this->goodsattr_model->join('typeattr','typeattr_taid','=','taid')->where("goods_gid={$gid}")->get();
		$dataattr = array();
		$dataspec = array();
		foreach($goodsAttrData as $k =>$v){
			$goodsAttrData[$k]['tavalue'] = explode('|', $v['tavalue']);
			if($v['class'] == '1'){
				$dataspec[$k] = $goodsAttrData[$k]; 
			}else{
				$dataattr[$k] = $goodsAttrData[$k];
			}
		}
		View::with('dataattr',$dataattr);
		View::with('dataspec',$dataspec);
		//p($dataattr);
		//p($dataspec);
		
		

		//分配数据
		View::with('detailOldData',$detailOldData);
		
		//p($detailOldData);exit;
		//载入模板
		View::make();
	}
	//异步删除列表图
	public function delListPic(){
		$pic = Q('post.pic',0);
		//得到当前ID
		$gid = Q('post.gid',0);
		//取出数据
		$goodsData = $this->goods_model->where("gid={$gid}")->get();
		if($goodsData[0]['list_pic'] == $pic){
			Db::table('goods')->where("gid",$gid)->update(['list_pic'=>'']);    //成功删除图片
		}
		//取出删除后值
		$goodsData = $this->goods_model->where("gid={$gid}")->get();
		echo json_encode($goodsData);exit;	
	}
	//异步删除详细页图册
	public function delDetailPic(){
		$gid = Q('post.gid',0);
		//取出数据     
		$detailOldData = $this->detail_model->where("goods_gid={$gid}")->get();
		Db::table('detail')->where("goods_gid",$gid)->update(['detail_pic'=>'']);    //成功删除图片
		$detailOldData = $this->detail_model->where("goods_gid={$gid}")->get();
		echo json_encode($detailOldData);exit;	
	}
	
} 
	
?>